package com.shanghai.shanghaielectric.communicate;

import android.content.Context;

//import com.shanghai.shanghaielectric.entity.CapacitorCountEntity;
import com.shanghai.shanghaielectric.entity.CapacitorEntity;
import com.shanghai.shanghaielectric.utils.CommUtil;

import java.util.List;

import shanghaielectric_lib.Utils.CRC16;

public class SerialAPI {


    public interface CheckCommunication<T> {
        void onupdate(boolean flag);
    }

    public static CheckCommunication mcheckCommunication;

    public static void getCheckCommunicationListener(CheckCommunication checkCommunication) {
        mcheckCommunication = checkCommunication;
    }

    private boolean IScommunication;
    private int count = 0;
//    private SerialPortAPI mPort;
//    private PlusAsyncTask mPlusTask;
    private List<OnSerialReceiveListener> mList;
    private final int INTEVAL = 50;
    private boolean isDestroy = false;

    private Context mContext;

//    private List<ABCValueEntity> mAcbEntitys;

//    private List<ErrRecordEntity> mErrEntitys;
//    private CapacitorCountEntity mCapacitorCountEntity;
    private CapacitorEntity mCapacitorEntity;

//    private CapacitorCountEntity mTotalCountEntity;

//    private ChartRealEntity mChartEntity;

    private double[] mParameter;

    private static SerialAPI sInstance = new SerialAPI();

    public static SerialAPI getInstance() {
        return sInstance;
    }

/*
    public void init(final Context context) {
        mContext = context;
        mCapacitorCountEntity = DbController.getInstance().queryCapacitor(
                CommUtil.getDayLongTime());
        if (mCapacitorCountEntity == null) {
            mCapacitorCountEntity = new CapacitorCountEntity();
            mCapacitorCountEntity.time = CommUtil.getDayLongTime();
            DbController.getInstance().insertCapacitorRecord(
                    mCapacitorCountEntity);
        }
        if (mTotalCountEntity == null) {
            mTotalCountEntity = ShareController.getInstance().getCapacitorCountEntity(context);
        }
        if (mCapacitorEntity == null) {
            mCapacitorEntity = new CapacitorEntity();
        }
        if (mErrEntitys == null) {
            mErrEntitys = ShareController.getInstance().getErrRecordList(context);
        }
        mList = new ArrayList<OnSerialReceiveListener>();
        mPort = new SerialPortAPI(Constans.DEVNUM, Constans.BAUDRATE,
                Constans.DATABITS, Constans.STOPBITS, Constans.PARITY);
        mPlusTask = new PlusAsyncTask() {
            private byte[] temp = new byte[0];

            @Override
            public Object doInBackground(Object... params) {
                Log.e("777", "whilestart----------------------");
                try {
                    while (mPort.isOpen) {
                        byte[] buf = mPort.receiveData();

//以下-------------------------------
//						boolean c= checkCommunicating(buf);//检查是否通讯
//以上----------------------------------

                        if (buf != null && buf.length > 0) {
                            Constans.ISCOMMUNICATION = true;
                            mcheckCommunication.onupdate(true);
                            count = 0;//通信计数重置
                            int cachingLenth = temp.length;
                            byte[] tempTemp = new byte[buf.length + cachingLenth];
                            for (int i = 0; i < cachingLenth; i++) {
                                tempTemp[i] = temp[i];
                            }
                            for (int i = 0; i < buf.length; i++) {
                                tempTemp[cachingLenth + i] = buf[i];
                            }
                            byte[] publishBuf = new byte[0];
                            int temptempLenth = tempTemp.length;

                            for (int i = 0; i < temptempLenth; i++) {
                                if (i >= 1 && tempTemp[i - 1] == 0x02) {

                                    if (tempTemp[i] == 0x03) {
                                        if ((temptempLenth - i + 2) >= 8) {
                                            publishBuf = new byte[8];
                                            for (int j = 0; j < 8; j++) {
                                                publishBuf[j] = tempTemp[i - 1 + j];
                                            }
                                            if (isCrc16(publishBuf)) {
                                                replyFirstMessage();
                                            }

                                            publishProgress(publishBuf);
                                            publishBuf = new byte[0];
                                            i = i + 6;
                                        } else {
                                            publishBuf = new byte[temptempLenth
                                                    - i + 1];
                                            for (int j = 0; j < temptempLenth
                                                    - i + 1; j++) {
                                                publishBuf[j] = tempTemp[i - 1
                                                        + j];
                                            }
                                            break;
                                        }
                                    } else if (tempTemp[i] == 0x10) {
                                        if ((temptempLenth - i + 2) >= 209) {
                                            publishBuf = new byte[209];
                                            for (int j = 0; j < 209; j++) {
                                                publishBuf[j] = tempTemp[i - 1
                                                        + j];
                                            }
                                            publishProgress(publishBuf);
                                            publishBuf = new byte[0];
                                            i = i + 207;
                                        } else {
                                            publishBuf = new byte[temptempLenth
                                                    - i + 1];
                                            for (int j = 0; j < temptempLenth
                                                    - i + 1; j++) {
                                                publishBuf[j] = tempTemp[i - 1
                                                        + j];
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                            temp = new byte[publishBuf.length];
                            for (int i = 0; i < temp.length; i++) {
                                temp[i] = publishBuf[i];
                            }
                        } else {
//                            Log.e("cccccccc",""+count);
                            count++;
                            if (count > Constans.FALSE_COMMUNICATION_COUNT) {
                                Constans.ISCOMMUNICATION = false;
                                mcheckCommunication.onupdate(false);
//Log.e("cccccccc",""+count);
                                count = 0;
                            }

                        }
                        try {
                            Thread.sleep(INTEVAL);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e("777", "task_while_err" + e.getMessage());
                }
                Log.e("777", "whileclose====================================");
                return null;
            }

            @Override
            public void onProgressUpdate(Object... params) {
                super.onProgressUpdate(params);
                try {
                    byte[] data = (byte[]) params[0];
                    parseData(data);
                } catch (Exception e) {
                }
            }

            @Override
            public void onPostExecute(Object result, String taskid) {
                super.onPostExecute(result, taskid);
                if (!isDestroy) {
                    mPlusTask.execute();
                }
            }

        };
        mPlusTask.execute();
    }

    public void setEntityTime() {
        Constans.isRunErr = 0;
        mCapacitorCountEntity = new CapacitorCountEntity();
        mCapacitorCountEntity.time = CommUtil.getDayLongTime();
        DbController.getInstance().insertCapacitorRecord(
                mCapacitorCountEntity);
    }

    public void onDestroy() {
        isDestroy = true;
        mPort.closeSerial();
        mList.clear();
    }
*/

/*    public void send(byte[] buf) {

//        mPort.sendData(buf);
// TODO: 2017/12/11  复位信号 下发===
        if (buf[3] != 0 && buf.length > 10) {
            Log.e("portAPI", buf[3] + "");
            Constans.commandBytes = buf;
            Constans.count = Constans.SEND_COMMAND_TIMES;
        }
        if (Constans.count > 0 && buf.length > 10) {
            mPort.sendData(Constans.commandBytes);
            Log.e("portAPI===", buf[3] + "---" + Constans.count);
            Log.e("portAPI====", MyArrayUtils.byteArrayToHexStr(Constans.commandBytes));

            Constans.count--;
        } else {
            mPort.sendData(buf);
            Log.e("portAPI====", MyArrayUtils.byteArrayToHexStr(buf));
        }

//        Log.e("portAPI", "" + MyArrayUtils.byteArrayToHexStr(buf));
    }

    public void send(String data) {
        mPort.sendData(data, "HEX");
    }*/

    public void registerListener(OnSerialReceiveListener listener) {
        try {
            if (listener != null && !mList.contains(listener)) {
                mList.add(listener);
            }
        } catch (Exception e) {
        }
    }

    public void removeListener(OnSerialReceiveListener listener) {
        if (mList.contains(listener)) {
            mList.remove(listener);
        }
    }

/*    private void updateErrChange(List<ErrRecordEntity> entitys) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onCurrentErrChange(entitys);
        }
    }

    private void updateBoardRunState(BoardStartState state) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onBoardRunState(state);
        }
    }

    private void updateABC(List<ABCValueEntity> entitys) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onRefreshABC(entitys);
        }
    }

    private void updateMain(double active, double reactive, double factor) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onRefreshMain(active, reactive, factor);
        }
    }

    private void updateVoltagePhaseSequence(VoltagePhaseSequence sequence) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onVoltagePhaseSequence(sequence);
        }
    }

    private void updateParameter(ParameterEntity entity) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onParameter(entity);
        }
    }*/

    private void updateCapacitor(CapacitorEntity entity) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onCapacitorState(entity);//onCapacitorState电容投切状态值
        }
    }

    private void updateActiveState(boolean isStart) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onActivityStart(isStart);
        }
    }

/*    private void updateRuningState(RuningState state) {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).onRunState(state);
        }
    }*/

/*    private void parseData(byte[] data) {

//		 Log.e("777", "publish=======parseData" + CommUtil.bytesToHexString(data));
        if (data.length < 2) {
            return;
        }
        if (data[0] == 0x02 && data[1] == 0x03) {
//			if (isCrc16(data)) {
//				replyFirstMessage();
//			}
        } else if (data[0] == 0x02 && data[1] == 0x10) {
            if (isCrc16(data)) {
                parseSecondData(data);
                replySecondMessage();

                //日志
                if (Calendar.getInstance().get(Calendar.MINUTE) == 44) {//每小时记录一次
                    TxtUtil.writeLogToFile2(Environment.getExternalStorageDirectory().getPath(), "运行日志", "通信正常");
                }
            }
        }
    }*/

/*  private void parseSecondData(byte[] data) {
        // 700
        int runState = CommUtil.twoByteToInt(data[7], data[8]);
        updateBoardRunState(BoardStartState.getEnum(runState));
        Log.e("boardState", "" + BoardStartState.getEnum(runState) + MyArrayUtils.byteArrayToHexStr(data));
        // 701
        countCapacitor(data[9], data[10]);

        //702
        boolean isActive = isActive(data[11], data[12]);
        mCapacitorEntity.isOpen = isActive;
        updateActiveState(isActive);
        if (mParameter == null) {
            mParameter = new double[MyApplication.getPointEntityList().size()];
        }
        setAbcData(data);
        updateABC(mAcbEntitys);
        // 716 717 718
        double active = CommUtil.twoByteToInt(data[39], data[40]) * 0.1;
        double reactive = CommUtil.twoByteToInt(data[41], data[42]) * 0.1;
        double factor = CommUtil.twoByteToInt(data[43], data[44]) * 0.001;
        updateMain(active, reactive, factor);
        mParameter[14] = CommUtil.round(active, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[15] = CommUtil.round(reactive, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[16] = CommUtil.round(factor, 2, BigDecimal.ROUND_HALF_UP);
        // 724
        updateVoltagePhaseSequence(VoltagePhaseSequence.getEnum(CommUtil
                .twoByteToInt(data[55], data[56])));

        // 742 743 744 745
        String errStr = "";
        errStr = errStr + getERR(data[91], data[92]);
        errStr = errStr + getERR(data[93], data[94]);
        errStr = errStr + getERR(data[95], data[96]);
        errStr = errStr + getERR(data[97], data[98]);
        Log.e("errorCode==", "" + getERR(data[97], data[98]));
        String errcode4 = "" + getERR(data[97], data[98]);
        Log.e("errorCode==", errcode4.substring(6, 7));
        //直接取到紧停为，将板子状态临时设置为故障状态，解决紧停无法复位的问题
        if (errcode4.substring(6, 7).equals("0")) {
            Constans.Is_E_STOP = false;
        } else {
            Constans.Is_E_STOP = true;
        }
        List<ErrRecordEntity> recoverErrEntitys = new ArrayList<ErrRecordEntity>();
        List<ErrRecordEntity> startErrEntitys = new ArrayList<ErrRecordEntity>();
        errData:
        for (int i = 0; i < errStr.length(); i++) {
            int indexData = Integer.parseInt(String.valueOf(errStr.charAt(i)));
            for (int j = 0; j < mErrEntitys.size(); j++) {
                if (ErrClassType.getEnum(i).msg.equals(mErrEntitys.get(j).content)) {
                    // TODO: 2017/12/11  测试=====
//                    if (i == 54) {
//
//                        if (ErrClassType.EMERGENCY_STOP.msg.equals(ErrClassType.getEnum(i).msg)) {
//                            Constans.Is_E_STOP = true;
//                            Log.e("errorTag===", "Constans.Is_E_STOP=true" + ErrClassType.getEnum(i).msg);
//                        } else {
////                            Log.e("errorTag===", "Constans.Is_E_STOP=false" + ErrClassType.getEnum(i).msg);
//                        }
//                    } else {
//                        Log.e("errorTag", "" + ErrClassType.getEnum(i).msg);
//
//                    }

                    if (indexData == 0) {
                        mErrEntitys.get(j).resetTime = CommUtil.getCurrentTime();
                        recoverErrEntitys.add(mErrEntitys.get(j));
                    }
                    continue errData;
                }
            }
            if (indexData == 1) {
                ErrRecordEntity entity = new ErrRecordEntity();
                entity.classContent = ErrClassType.getEnum(i).classMsg;
                entity.content = ErrClassType.getEnum(i).msg;
                entity.startTime = CommUtil.getCurrentTime();
                startErrEntitys.add(entity);
            }
        }
        if (recoverErrEntitys.size() > 0 || startErrEntitys.size() > 0) {
            DbController.getInstance().updateErrRecord(recoverErrEntitys);
            DbController.getInstance().InsertErrRecord(startErrEntitys);
            for (int i = 0; i < recoverErrEntitys.size(); i++) {
                mErrEntitys.remove(recoverErrEntitys.get(i));
            }
            mErrEntitys.addAll(startErrEntitys);
            if (mContext != null) {
                ShareController.getInstance().updateErrRecordList(mContext, mErrEntitys);
            }
            updateErrChange(mErrEntitys);
        }
        //767 - 792
        ParameterEntity parameterEntity = new ParameterEntity();
        parameterEntity.currentTransformerRatio = CommUtil.twoByteToInt(data[141], data[142]);
        parameterEntity.overvoltageProtection = CommUtil.twoByteToInt(data[143], data[144]);
        parameterEntity.undervoltageProtection = CommUtil.twoByteToInt(data[145], data[146]);
        parameterEntity.overcurrentProtection = CommUtil.twoByteToInt(data[147], data[148]);
        parameterEntity.harmonicVoltageLimited = CommUtil.twoByteToInt(data[149], data[150]);
        parameterEntity.harmonicCurrentLimited = CommUtil.twoByteToInt(data[151], data[152]);
        parameterEntity.capacitorSwitchingDelay = CommUtil.twoByteToInt(data[153], data[154]);

        parameterEntity.passiveComponentType = CommUtil.twoByteToInt(data[155], data[156]);
        mCapacitorEntity.mPassiveType = parameterEntity.passiveComponentType;

        parameterEntity.externalTransformer = CommUtil.twoByteToInt(data[157], data[158]);
        parameterEntity.CapacitorSwitchingThreshold = CommUtil.twoByteToInt(data[159], data[160]);
        parameterEntity.runModel = CommUtil.twoByteToInt(data[161], data[162]);
        parameterEntity.reactiveOutputRatio = CommUtil.twoByteToInt(data[163], data[164]);

        parameterEntity.wholeCabinetTransformerRatio = CommUtil.twoByteToInt(data[165], data[166]);
        parameterEntity.highHarmonicSelection = CommUtil.twoByteToInt(data[189], data[190]);
        ParameterSetController.getInstance().setBoardParameter(parameterEntity);
        updateParameter(parameterEntity);
        updateCapacitor(mCapacitorEntity);

        byte[] stateByte = new byte[2];
        stateByte[0] = data[201];
        stateByte[1] = data[202];
        updateRuningState(RuningState.getEnum(CommUtil.bytesToHexString(stateByte)));


    }

    private String getERR(byte first, byte second) {
        byte[] errData = new byte[2];
        errData[0] = first;
        errData[1] = second;
        String err = CommUtil.hexString2binaryString(CommUtil
                .bytesToHexString(errData));
        String temp = "";
        for (int i = 0; i < err.length(); i++) {
            temp = temp + String.valueOf(err.charAt(err.length() - i - 1));
        }
        return temp;
    }*/

  /*  private void setAbcData(byte[] data) {
        if (mAcbEntitys == null || mAcbEntitys.size() <= 0) {
            mAcbEntitys = new ArrayList<ABCValueEntity>();
            for (int i = 0; i < 12; i++) {
                ABCValueEntity entity = new ABCValueEntity();
                mAcbEntitys.add(entity);
            }
        }
        if (mChartEntity == null) {
            mChartEntity = new ChartRealEntity();
        }
        mChartEntity.chartTime = System.currentTimeMillis();
        mAcbEntitys.get(0).name = "网侧电压(V)";
        // 703 704 705
        mAcbEntitys.get(0).aValue = CommUtil.twoByteToInt(data[13], data[14]) * 0.1;
        mAcbEntitys.get(0).bValue = CommUtil.twoByteToInt(data[15], data[16]) * 0.1;
        mAcbEntitys.get(0).cValue = CommUtil.twoByteToInt(data[17], data[18]) * 0.1;
        mAcbEntitys.get(0).aDot = 1;
        mAcbEntitys.get(0).bDot = 1;
        mAcbEntitys.get(0).cDot = 1;
        mParameter[0] = CommUtil.round(mAcbEntitys.get(0).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[1] = CommUtil.round(mAcbEntitys.get(0).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[2] = CommUtil.round(mAcbEntitys.get(0).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(1).name = "网侧有功功率(kW)";
        // 725 726 727
        mAcbEntitys.get(1).aValue = CommUtil.twoByteToInt(data[57], data[58]) * 0.1;
        mAcbEntitys.get(1).bValue = CommUtil.twoByteToInt(data[59], data[60]) * 0.1;
        mAcbEntitys.get(1).cValue = CommUtil.twoByteToInt(data[61], data[62]) * 0.1;
        mAcbEntitys.get(1).aDot = 1;
        mAcbEntitys.get(1).bDot = 1;
        mAcbEntitys.get(1).cDot = 1;
        mChartEntity.aPhasePowerActive = mAcbEntitys.get(1).aValue;
        mChartEntity.bPhasePowerActive = mAcbEntitys.get(1).bValue;
        mChartEntity.cPhasePowerActive = mAcbEntitys.get(1).cValue;
        mParameter[19] = CommUtil.round(mAcbEntitys.get(1).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[20] = CommUtil.round(mAcbEntitys.get(1).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[21] = CommUtil.round(mAcbEntitys.get(1).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(2).name = "网侧电流(A)";
        // 706 707 708
        mAcbEntitys.get(2).aValue = CommUtil.twoByteToInt(data[19], data[20]) * 0.1;
        mAcbEntitys.get(2).bValue = CommUtil.twoByteToInt(data[21], data[22]) * 0.1;
        mAcbEntitys.get(2).cValue = CommUtil.twoByteToInt(data[23], data[24]) * 0.1;
        mAcbEntitys.get(2).aDot = 1;
        mAcbEntitys.get(2).bDot = 1;
        mAcbEntitys.get(2).cDot = 1;
        mParameter[3] = CommUtil.round(mAcbEntitys.get(2).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[4] = CommUtil.round(mAcbEntitys.get(2).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[5] = CommUtil.round(mAcbEntitys.get(2).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(3).name = "网侧无功功率(kVar)";
        // 728 729 730
        mAcbEntitys.get(3).aValue = CommUtil.twoByteToInt(data[63], data[64]) * 0.1;
        mAcbEntitys.get(3).bValue = CommUtil.twoByteToInt(data[65], data[66]) * 0.1;
        mAcbEntitys.get(3).cValue = CommUtil.twoByteToInt(data[67], data[68]) * 0.1;
        mAcbEntitys.get(3).aDot = 1;
        mAcbEntitys.get(3).bDot = 1;
        mAcbEntitys.get(3).cDot = 1;
        mChartEntity.aPhasePowerRective = mAcbEntitys.get(3).aValue;
        mChartEntity.bPhasePowerRective = mAcbEntitys.get(3).bValue;
        mChartEntity.cPhasePowerRective = mAcbEntitys.get(3).cValue;
        mParameter[23] = CommUtil.round(mAcbEntitys.get(3).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[24] = CommUtil.round(mAcbEntitys.get(3).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[25] = CommUtil.round(mAcbEntitys.get(3).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(4).name = "负载电流(A)";
        // 709 710 711
        mAcbEntitys.get(4).aValue = CommUtil.twoByteToInt(data[25], data[26]) * 0.1;
        mAcbEntitys.get(4).bValue = CommUtil.twoByteToInt(data[27], data[28]) * 0.1;
        mAcbEntitys.get(4).cValue = CommUtil.twoByteToInt(data[29], data[30]) * 0.1;
        mAcbEntitys.get(4).aDot = 1;
        mAcbEntitys.get(4).bDot = 1;
        mAcbEntitys.get(4).cDot = 1;
        mParameter[6] = CommUtil.round(mAcbEntitys.get(4).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[7] = CommUtil.round(mAcbEntitys.get(4).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[8] = CommUtil.round(mAcbEntitys.get(4).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(5).name = "网侧功率因数";
        // 731 732 733
        mAcbEntitys.get(5).aValue = CommUtil.twoByteToInt(data[69], data[70]) * 0.001;
        mAcbEntitys.get(5).bValue = CommUtil.twoByteToInt(data[71], data[72]) * 0.001;
        mAcbEntitys.get(5).cValue = CommUtil.twoByteToInt(data[73], data[74]) * 0.001;
        mAcbEntitys.get(5).aDot = 2;
        mAcbEntitys.get(5).bDot = 2;
        mAcbEntitys.get(5).cDot = 2;
        mChartEntity.aPhasePowerFactor = mAcbEntitys.get(5).aValue;
        mChartEntity.bPhasePowerFactor = mAcbEntitys.get(5).bValue;
        mChartEntity.cPhasePowerFactor = mAcbEntitys.get(5).cValue;
        mParameter[26] = CommUtil.round(mAcbEntitys.get(5).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[27] = CommUtil.round(mAcbEntitys.get(5).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[28] = CommUtil.round(mAcbEntitys.get(5).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(6).name = "补偿电流(A)";
        // 712 713 714
        mAcbEntitys.get(6).aValue = CommUtil.twoByteToInt(data[31], data[32]) * 0.1;
        mAcbEntitys.get(6).bValue = CommUtil.twoByteToInt(data[33], data[34]) * 0.1;
        mAcbEntitys.get(6).cValue = CommUtil.twoByteToInt(data[35], data[36]) * 0.1;
        mAcbEntitys.get(6).aDot = 1;
        mAcbEntitys.get(6).bDot = 1;
        mAcbEntitys.get(6).cDot = 1;
        mParameter[9] = CommUtil.round(mAcbEntitys.get(6).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[10] = CommUtil.round(mAcbEntitys.get(6).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[11] = CommUtil.round(mAcbEntitys.get(6).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(7).name = "负载功率因数";
        // 734 735 736
        mAcbEntitys.get(7).aValue = CommUtil.twoByteToInt(data[75], data[76]) * 0.001;
        mAcbEntitys.get(7).bValue = CommUtil.twoByteToInt(data[77], data[78]) * 0.001;
        mAcbEntitys.get(7).cValue = CommUtil.twoByteToInt(data[79], data[80]) * 0.001;
        mAcbEntitys.get(7).aDot = 2;
        mAcbEntitys.get(7).bDot = 2;
        mAcbEntitys.get(7).cDot = 2;
        mChartEntity.aPhaseLoadPowerFactor = mAcbEntitys.get(7).aValue;
        mChartEntity.bPhaseLoadPowerFactor = mAcbEntitys.get(7).bValue;
        mChartEntity.cPhaseLoadPowerFactor = mAcbEntitys.get(7).cValue;
        mParameter[29] = CommUtil.round(mAcbEntitys.get(7).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[30] = CommUtil.round(mAcbEntitys.get(7).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[31] = CommUtil.round(mAcbEntitys.get(7).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(8).name = "补偿电流负载率(%)";
        // 719 720 721
        mAcbEntitys.get(8).aValue = CommUtil.twoByteToInt(data[45], data[46]);
        mAcbEntitys.get(8).bValue = CommUtil.twoByteToInt(data[47], data[48]);
        mAcbEntitys.get(8).cValue = CommUtil.twoByteToInt(data[49], data[50]);
        mParameter[16] = CommUtil.round(mAcbEntitys.get(8).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[17] = CommUtil.round(mAcbEntitys.get(8).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[18] = CommUtil.round(mAcbEntitys.get(8).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(9).name = "有源中线电流(A)";
        mAcbEntitys.get(9).secondName = "P端(V)";
        // 722 737
        mAcbEntitys.get(9).aValue = CommUtil.twoByteToInt(data[51], data[52]) * 0.1;
        mAcbEntitys.get(9).bValue = CommUtil.twoByteToInt(data[81], data[82]) * 0.1;
        mAcbEntitys.get(9).aDot = 1;
        mAcbEntitys.get(9).bDot = 1;
        mParameter[19] = CommUtil.round(mAcbEntitys.get(9).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[32] = CommUtil.round(mAcbEntitys.get(9).bValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(10).name = "I GBT温度(℃)";
        // 739 740 741
        mAcbEntitys.get(10).aValue = CommUtil.twoByteToInt(data[85], data[86]);
        mAcbEntitys.get(10).bValue = CommUtil.twoByteToInt(data[87], data[88]);
        mAcbEntitys.get(10).cValue = CommUtil.twoByteToInt(data[89], data[90]);
        mParameter[34] = CommUtil.round(mAcbEntitys.get(10).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[35] = CommUtil.round(mAcbEntitys.get(10).bValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[36] = CommUtil.round(mAcbEntitys.get(10).cValue, 2, BigDecimal.ROUND_HALF_UP);

        mAcbEntitys.get(11).name = "电压频率(Hz)";
        mAcbEntitys.get(11).secondName = "N端(V)";
        // 715 738
        mAcbEntitys.get(11).aValue = CommUtil.twoByteToInt(data[37], data[38]) * 0.01;
        mAcbEntitys.get(11).bValue = CommUtil.twoByteToInt(data[83], data[84]) * 0.1;
        mAcbEntitys.get(11).aDot = 2;
        mAcbEntitys.get(11).bDot = 1;
        mParameter[12] = CommUtil.round(mAcbEntitys.get(11).aValue, 2, BigDecimal.ROUND_HALF_UP);
        mParameter[33] = CommUtil.round(mAcbEntitys.get(11).bValue, 2, BigDecimal.ROUND_HALF_UP);
    }*/

    private boolean isActive(byte first, byte second) {
        byte[] data = new byte[2];
        data[0] = first;
        data[1] = second;
        String dataStr = CommUtil.hexString2binaryString(CommUtil
                .bytesToHexString(data));
        int indexData = Integer.parseInt(String.valueOf(dataStr.charAt(7)));
        boolean isStart = (indexData == 0 ? false : true);
        return isStart;
    }

 /*   private void countCapacitor(byte first, byte second) {
        byte[] data = new byte[2];
        data[0] = first;
        data[1] = second;
        String dataStr = CommUtil.hexString2binaryString(CommUtil
                .bytesToHexString(data));
        int indexData = Integer.parseInt(String.valueOf(dataStr.charAt(15)));
        if (mCapacitorEntity.firstAuxiliary != indexData) {
            if (mCapacitorEntity.firstAuxiliary == 0) {
                mCapacitorCountEntity.firstAuxiliary = mCapacitorCountEntity.firstAuxiliary + 1;
                mTotalCountEntity.firstAuxiliary = mTotalCountEntity.firstAuxiliary + 1;
            }
            mCapacitorEntity.firstAuxiliary = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(14)));
        if (mCapacitorEntity.secondAuxiliary != indexData) {
            if (mCapacitorEntity.secondAuxiliary == 0) {
                mCapacitorCountEntity.secondAuxiliary = mCapacitorCountEntity.secondAuxiliary + 1;
                mTotalCountEntity.secondAuxiliary = mTotalCountEntity.secondAuxiliary + 1;
            }
            mCapacitorEntity.secondAuxiliary = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(13)));
        if (mCapacitorEntity.thirdAuxiliary != indexData) {
            if (mCapacitorEntity.thirdAuxiliary == 0) {
                mCapacitorCountEntity.thirdAuxiliary = mCapacitorCountEntity.thirdAuxiliary + 1;
                mTotalCountEntity.thirdAuxiliary = mTotalCountEntity.thirdAuxiliary + 1;
            }
            mCapacitorEntity.thirdAuxiliary = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(12)));
        if (mCapacitorEntity.fourthAuxiliary != indexData) {
            if (mCapacitorEntity.fourthAuxiliary == 0) {
                mCapacitorCountEntity.fourthAuxiliary = mCapacitorCountEntity.fourthAuxiliary + 1;
                mTotalCountEntity.fourthAuxiliary = mTotalCountEntity.fourthAuxiliary + 1;
            }
            mCapacitorEntity.fourthAuxiliary = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(11)));
        if (mCapacitorEntity.firstASub != indexData) {
            if (mCapacitorEntity.firstASub == 0) {
                mCapacitorCountEntity.firstASub = mCapacitorCountEntity.firstASub + 1;
                mTotalCountEntity.firstASub = mTotalCountEntity.firstASub + 1;
            }
            mCapacitorEntity.firstASub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(10)));
        if (mCapacitorEntity.firstBSub != indexData) {
            if (mCapacitorEntity.firstBSub == 0) {
                mCapacitorCountEntity.firstBSub = mCapacitorCountEntity.firstBSub + 1;
                mTotalCountEntity.firstBSub = mTotalCountEntity.firstBSub + 1;
            }
            mCapacitorEntity.firstBSub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(9)));
        if (mCapacitorEntity.firstCSub != indexData) {
            if (mCapacitorEntity.firstCSub == 0) {
                mCapacitorCountEntity.firstCSub = mCapacitorCountEntity.firstCSub + 1;
                mTotalCountEntity.firstCSub = mTotalCountEntity.firstCSub + 1;
            }
            mCapacitorEntity.firstCSub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(8)));
        if (mCapacitorEntity.secondASub != indexData) {
            if (mCapacitorEntity.secondASub == 0) {
                mCapacitorCountEntity.secondASub = mCapacitorCountEntity.secondASub + 1;
                mTotalCountEntity.secondASub = mTotalCountEntity.secondASub + 1;
            }
            mCapacitorEntity.secondASub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(7)));
        if (mCapacitorEntity.secondBSub != indexData) {
            if (mCapacitorEntity.secondBSub == 0) {
                mCapacitorCountEntity.secondBSub = mCapacitorCountEntity.secondBSub + 1;
                mTotalCountEntity.secondBSub = mTotalCountEntity.secondBSub + 1;
            }
            mCapacitorEntity.secondBSub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(6)));
        if (mCapacitorEntity.secondCSub != indexData) {
            if (mCapacitorEntity.secondCSub == 0) {
                mCapacitorCountEntity.secondCSub = mCapacitorCountEntity.secondCSub + 1;
                mTotalCountEntity.secondCSub = mTotalCountEntity.secondCSub + 1;
            }
            mCapacitorEntity.secondCSub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(5)));
        if (mCapacitorEntity.thirdASub != indexData) {
            if (mCapacitorEntity.thirdASub == 0) {
                mCapacitorCountEntity.thirdASub = mCapacitorCountEntity.thirdASub + 1;
                mTotalCountEntity.thirdASub = mTotalCountEntity.thirdASub + 1;
            }
            mCapacitorEntity.thirdASub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(4)));
        if (mCapacitorEntity.thirdBSub != indexData) {
            if (mCapacitorEntity.thirdBSub == 0) {
                mCapacitorCountEntity.thirdBSub = mCapacitorCountEntity.thirdBSub + 1;
                mTotalCountEntity.thirdBSub = mTotalCountEntity.thirdBSub + 1;
            }
            mCapacitorEntity.thirdBSub = indexData;
        }
        indexData = Integer.parseInt(String.valueOf(dataStr.charAt(3)));
        if (mCapacitorEntity.thirdCSub != indexData) {
            if (mCapacitorEntity.thirdCSub == 0) {
                mCapacitorCountEntity.thirdCSub = mCapacitorCountEntity.thirdCSub + 1;
                mTotalCountEntity.thirdCSub = mTotalCountEntity.thirdCSub + 1;
            }
            mCapacitorEntity.thirdCSub = indexData;
        }
        ShareController.getInstance().updateCapacitorCountEntity(mContext, mTotalCountEntity);
        DbController.getInstance().insertCapacitorRecord(mCapacitorCountEntity);
    }*/

/*    private void replyFirstMessage() {
        byte[] sendBuf = ParameterSetController.getInstance().getReplyFirstMsg();
        int crcValue = CRC16.calcCrc16(sendBuf);
        byte[] sendData = new byte[sendBuf.length + 2];
        for (int i = 0; i < sendBuf.length; i++) {
            sendData[i] = sendBuf[i];
        }
        byte[] crcbyte = CommUtil.HexString2Bytes(CommUtil.IntToTwoByteHexString(crcValue));
        sendData[sendBuf.length] = crcbyte[0];
        sendData[sendBuf.length + 1] = crcbyte[1];
        send(sendData);
    }

    private void replySecondMessage() {
        byte[] sendBuf = CommUtil.HexString2Bytes("021002BC0064");
        int crcValue = CRC16.calcCrc16(sendBuf);
        byte[] sendData = new byte[sendBuf.length + 2];
        for (int i = 0; i < sendBuf.length; i++) {
            sendData[i] = sendBuf[i];
        }
        byte[] crcbyte = CommUtil.HexString2Bytes(CommUtil.IntToTwoByteHexString(crcValue));
        sendData[sendBuf.length] = crcbyte[0];
        sendData[sendBuf.length + 1] = crcbyte[1];

        send(sendData);
    }*/

    private boolean isCrc16(byte[] data) {
        int dataLength = data.length;
        if (dataLength < 2) {
            return false;
        }
        int aa = CRC16.calcCrc16(data, 0, dataLength - 2);
        byte[] crcBytes = new byte[2];
        crcBytes[0] = data[dataLength - 2];
        crcBytes[1] = data[dataLength - 1];
        int bb = Integer.parseInt(CommUtil.bytesToHexString(crcBytes).trim(),
                16);
        return aa == bb;
    }

/*    public List<ErrRecordEntity> getCurrentErr() {
        return mErrEntitys;
    }

    public ChartRealEntity getChartRealEntity() {
        return mChartEntity;
    }*/

    public CapacitorEntity getCapacitorEntity() {
        return mCapacitorEntity;
    }

    public double[] getWebParameter() {
        return mParameter;
    }

/*    private boolean checkCommunicating(final byte[] buf) {


        if (buf != null && buf.length > 0) {

            Constans.ISCOMMUNICATION = true;
            return true;
        } else {
            Constans.ISCOMMUNICATION = false;
            return false;
        }

    }*/


}

